package com.briup.auth.service;

import com.briup.auth.constant.AuthConstant;
import com.briup.common.cache.CacheService;
import com.briup.common.sms.RandomUtil;
import com.briup.common.sms.ZhenZiYunSmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Description 短信验证码相关操作
 * @Author wood
 */
@Service
public class SmsVerifyCodeService {

    @Autowired
    private CacheService cacheService;

    /**
     * 发送短信验证码
     * @param telephone 手机号
     * @return
     */
    public boolean sendCode(String telephone){
        String random = RandomUtil.getRandomNum(6);
        Map<String,String> map = new HashMap<>();
        // 验证码
        map.put("code",random);
        // 有效时间，单位分钟，当前阿里云模板无效，榛子云模板有效
        map.put("time",String.valueOf(AuthConstant.TELEPHONE_CODE_EXPIRE_SECONDS_REDIS/60));
        cacheService.setEx(telephone,random, AuthConstant.TELEPHONE_CODE_EXPIRE_SECONDS_REDIS, TimeUnit.SECONDS);
        return ZhenZiYunSmsUtil.sendSms(telephone,map);
//        return AliyunSmsUtil.sendSms(telephone,map);
    }
}
